﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using md.imi.membrane.constants;
using md.imi.membranes.objects.rules;
using md.imi.membranes.objects.membranes;
    
namespace md.imi.membranes.gui.Editor
{
    public partial class RightHandReplicationRuleEdit : Form
    {
        private RightHandRule   mRigthHandRule;
        private string          mParentRuleLabel;
        private string          mTargetMembraneLabel;
        private MembranesTree   mParentMembrane;
        
        public RightHandReplicationRuleEdit()
        {
            InitializeComponent();
        }

        public RightHandReplicationRuleEdit(string parentRuleLabel, RightHandRule rigthHandRule,
                                           MembranesTree parentMembrane)
            : this()
        {
            mParentRuleLabel= parentRuleLabel;
            mRigthHandRule  = rigthHandRule;
            mParentMembrane = parentMembrane;

            lblInfo.Text = String.Format("Edit rule '{0}'", parentRuleLabel);

            rbIn.Checked = (rigthHandRule.TargetType == RuleTargets.In);
            rbHere.Checked = (rigthHandRule.TargetType == RuleTargets.Here);
            rbOut.Checked = (rigthHandRule.TargetType == RuleTargets.Out);

            mTargetMembraneLabel = rigthHandRule.TargetMembraneLabel;
            if ((mTargetMembraneLabel != null) && (mTargetMembraneLabel.Length > 0) && (rigthHandRule.TargetType == RuleTargets.In))
                tbTargetMembraneLabel.Text = String.Format("[{0}]", mTargetMembraneLabel);

            ReloadMultisets();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            var me = new MultisetEditor("");
            me.SetInfoText("Resulting output for rule " + mParentRuleLabel);

            me.LoadData(mRigthHandRule.Result);
            if (me.ShowDialog() == DialogResult.OK)
            {
                mRigthHandRule.Result = me.GetData();
                ReloadMultisets();
            }
        }

        private void ReloadMultisets()
        {
            // check if this is a new rule
            if (mRigthHandRule.Result != null)
            {
                tbRuleOutput.Text = mRigthHandRule.Result.ToString();
            }
        }

        public RightHandRule GetData()
        {
            if (rbIn.Checked)
            {
                mRigthHandRule.TargetType = RuleTargets.In;
            }
            else if (rbOut.Checked)
            {
                mRigthHandRule.TargetType = RuleTargets.Out;
            }
            else if (rbHere.Checked)
            {
                mRigthHandRule.TargetType = RuleTargets.Here;
            }


            mRigthHandRule.TargetMembraneLabel = mTargetMembraneLabel;//tbTargetMembraneLabel.Text;
            return mRigthHandRule;
        }

        private void rbIn_CheckedChanged(object sender, EventArgs e)
        {
            btnEditTargetMembrane.Enabled = rbIn.Checked;
        }
        
        void BtnEditTargetMembraneClick(object sender, EventArgs e)
        {
            // show membranes selection dialog (show all inner membranes)
            TargetMembraneListSelect tmls = new TargetMembraneListSelect();
            tmls.SetInfoText("Select target membrane for rule " + mParentRuleLabel);
            tmls.SetInnerMembranesList(mParentMembrane.Membranes, mTargetMembraneLabel);
            
            if (tmls.ShowDialog() == DialogResult.Yes ) {
                mTargetMembraneLabel = tmls.GetData().Label;
                tbTargetMembraneLabel.Text = String.Format("[{0}]", mTargetMembraneLabel);
                mRigthHandRule.TargetMembraneLabel = String.Format("[{0}]", mTargetMembraneLabel);
            }
        }
    }
}
